package com.example.mylibrarybase.yumengmeng09.ui;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.bar.initAction_Bar;
import com.example.mylibrarybase.base.baseui.BaseActivity;
import com.example.mylibrarybase.yumengmeng09.view.LoadingView_09;

import java.util.ArrayList;

public class MainActivity_loadView_09 extends BaseActivity implements LoadingView_09.OnRefreshListener, View.OnClickListener {

    private initAction_Bar mBase_action_bar;
    private LoadingView_09 mLoading_view;
    private TextView mLoading_view_tv1;
    private TextView mLoading_view_tv2;
    private TextView mLoading_view_tv3;
    private TextView mLoading_view_tv4;
    private int mMLoading_view_tv1_id1;
    private int mMLoading_view_tv1_id2;
    private int mMLoading_view_tv1_id3;
    private int mMLoading_view_tv1_id4;
    private Handler mHandler;
    private Runnable mRunnable;
    private ArrayList<TextView> mTextViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public int getlayouXML() {
        return R.layout.activity_main_load_view_09;
    }

    @Override
    public void initView() {
        mBase_action_bar = (initAction_Bar) findViewById(R.id.base_action_bar);
        mBase_action_bar
                .setMode(initAction_Bar.AUTO_ONCLICK_MODE)
                .setCallBack(new initAction_Bar.Action_bar_call_back() {
                    @Override
                    public void getAction_barView_backbutton(TextView textView) {

                    }

                    @Override
                    public void getAction_barView_backbutton_icon(ImageView imageView) {

                    }

                    @Override
                    public void getAction_barView_title(TextView textView) {
                        textView.setText("loadingView");
                    }

                    @Override
                    public void getAction_barView_right_icon(ImageView imageView) {

                    }
                });

        mLoading_view = (LoadingView_09) findViewById(R.id.loading_view);
        mLoading_view.setRefrechListener(this);

        mLoading_view_tv1 = (TextView) findViewById(R.id.loading_view_tv1);
        mLoading_view_tv2 = (TextView) findViewById(R.id.loading_view_tv2);
        mLoading_view_tv3 = (TextView) findViewById(R.id.loading_view_tv3);
        mLoading_view_tv4 = (TextView) findViewById(R.id.loading_view_tv4);
        mMLoading_view_tv1_id1 = mLoading_view_tv1.getId();
        mMLoading_view_tv1_id2 = mLoading_view_tv2.getId();
        mMLoading_view_tv1_id3 = mLoading_view_tv3.getId();
        mMLoading_view_tv1_id4 = mLoading_view_tv4.getId();


        mLoading_view_tv1.setOnClickListener(this);
        mLoading_view_tv2.setOnClickListener(this);
        mLoading_view_tv3.setOnClickListener(this);
        mLoading_view_tv4.setOnClickListener(this);

        mTextViews = new ArrayList<>();
        mTextViews.add(mLoading_view_tv1);
        mTextViews.add(mLoading_view_tv2);
        mTextViews.add(mLoading_view_tv3);
        mTextViews.add(mLoading_view_tv4);
    }
    public void setTextViewclick(TextView textView){

        for (int i = 0; i < mTextViews.size(); i++) {
            mTextViews.get(i).setEnabled(false);
            if (textView ==mTextViews.get(i)){
                mTextViews.get(i).setEnabled(true);
            }
        }
    }
    public void setTextViewclickok(){
        for (int i = 0; i < mTextViews.size(); i++) {
            mTextViews.get(i).setEnabled(true);
        }
    }
    @Override
    public void initData() {

    }

    @Override
    public void refresh() {
        mLoading_view.setStatue(LoadingView_09.LOADING);
        mRunnable = new Runnable() {
            @Override
            public void run() {
                mLoading_view.setStatue(LoadingView_09.NO_BUY);
            }
        };
        mHandler.postDelayed(mRunnable,3000);

    }

    @Override
    public void onClick(View view) {
                if(!mLoading_view.isRuning()){
                    setTextData(view);
                }
    }

    private void setTextData(View view) {
        mLoading_view.setStatue(LoadingView_09.LOADING);
        mHandler = new Handler();
        if (view.getId()==mMLoading_view_tv1_id1){
            setTextViewclick(mLoading_view_tv1);
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    mLoading_view.setStatue(LoadingView_09.GONE);
                    setTextViewclickok();
                }
            };
            mHandler.postDelayed(mRunnable,3000);
        }else if (view.getId()==mMLoading_view_tv1_id2){
            setTextViewclick(mLoading_view_tv2);
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    mLoading_view.setStatue(LoadingView_09.NO_BUY);
                    setTextViewclickok();
                }
            };
            mHandler.postDelayed(mRunnable,3000);
        }else if (view.getId()==mMLoading_view_tv1_id3){
            setTextViewclick(mLoading_view_tv3);
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    mLoading_view.setStatue(LoadingView_09.NO_NETWORK);
                    setTextViewclickok();
                }
            };
            mHandler.postDelayed(mRunnable,3000);
        }else if (view.getId()==mMLoading_view_tv1_id4){
            setTextViewclick(mLoading_view_tv4);
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    mLoading_view.setStatue(LoadingView_09.NO_DATA);
                    setTextViewclickok();
                }
            };
            mHandler.postDelayed(mRunnable,3000);
        }
    }
}
